//
//  ThemeManager.m
//  Weibo
//
//  Created by taoshanchang on 15/1/25.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "ThemeManager.h"
#import "CONSTS.h"


static ThemeManager *sigleton = nil;

@implementation ThemeManager

+(ThemeManager *)sharedInstance{
    if(sigleton == nil){
        @synchronized(self){
            sigleton = [[ThemeManager alloc] init];
        }
    }

    return sigleton;
}

-(id)init{
    self = [super init];
    if(self){
        NSString *themePath =  [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
        self.themePlist = [NSDictionary dictionaryWithContentsOfFile:themePath];
        self.themeName = nil;
    }
    return self;
}

-(void)setThemeName:(NSString *)themeName{
    if (_themeName != themeName) {
        _themeName = themeName;
    }
    NSString *themeFontColorDir = [self getThemePath];
    NSString *filePath = [themeFontColorDir stringByAppendingPathComponent:@"fontColor.plist"];
    self.fontColorPlist = [NSDictionary dictionaryWithContentsOfFile:filePath];

}

-(NSString *)getThemePath{
    //项目的根路径
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    if(self.themeName == nil){
        return resourcePath;
    }
    //获取主题的相对路径
    NSString *themePath = [_themePlist objectForKey:_themeName];
    //拼装完整路径
    NSString *path = [resourcePath stringByAppendingString:themePath];
    return path;
}

-(UIImage *)getThemeImage:(NSString *)imageName{
    if(imageName.length == 0){
        return nil;
    }
    NSString *themePath = [self getThemePath];
    
    NSString *imagePath = [themePath stringByAppendingFormat:@"/%@",imageName];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    return image;
}

-(UIColor *)getColorWithName:(NSString *)colorName{
    if(colorName.length==0||colorName == nil){
        return nil;
    }
    NSString *rgbColor = [_fontColorPlist objectForKey:colorName];
    NSArray *rgbArray = [rgbColor componentsSeparatedByString:@","];
    if (rgbArray.count == 3) {
        float r = [rgbArray[0] floatValue];
        float g = [rgbArray[1] floatValue];
        float b = [rgbArray[2] floatValue];
        return Color(r, g, b, 1);
    }
    return nil; 
    
}


@end
